import GameDataMgr from "./GameDataMgr"
import MatchGameData from "./MatchGameData"
import PageMgr from "./PageMgr"
import EventMgr from "./EventMgr"
import GameEventType from "./GameEventType"
import SdkHelper from "./SdkHelper"
import SystemConfig from "./SystemConfig"

export default class FrameworkManager {
    static _interface = null;

    static _getInterface() {
        if (!FrameworkManager._interface) {
            FrameworkManager._interface = new FrameworkManager();
        }
        return FrameworkManager._interface;
    }

    static log(...str) {
        // console.log(str);
        if (MatchGameData.isEnableDebug()) {
            var args = Array.prototype.slice.call(arguments);
            if (cc.sys.isNative) {
                try {
                    var a = JSON.stringify(args);
                    console.log(SystemConfig.GAME_NAME, a);
                    this.saveLog(a);
                } catch (err) {
                    console.error(SystemConfig.GAME_NAME, err);
                }
            } else {
                console.log(SystemConfig.GAME_NAME, args);
            }
        }
    }

    static error(...str) {
        console.error(str);
        if (MatchGameData.isEnableDebug()) {
            var args = Array.prototype.slice.call(arguments);
            if (cc.sys.isNative) {
                try {
                    var a = JSON.stringify(args);
                    console.error(SystemConfig.GAME_NAME, a);
                    this.saveLog(a);
                } catch (err) {
                    console.error(SystemConfig.GAME_NAME, err);
                }
            } else {
                console.error(args);
            }
        }
    }

    static saveLog(log) {
        // if (GameDataMgr.reportErrorQueue.size() > 400) {
        //     GameDataMgr.reportErrorQueue.dequeue();
        // }
        // log && GameDataMgr.reportErrorQueue.enqueue(log);
    }

    static httpErr(err, callback) {
        this.log(err);
        SdkHelper.showToast("网络异常,请检查网络");
        SdkHelper.reportData("httpErr", {
            response: JSON.stringify(err)
        });
        PageMgr.showPage("ReconnectPage", {
            name: "ReconnectPage",
            data: {
                callback: callback
            }
        });
    }

    static reconnectSuc() {
        PageMgr.hidePage("LoadingPage");
        EventMgr.trigger(GameEventType.CLOSE_RECONNECT);
    }

    static reconnectFai() {
        PageMgr.hidePage("LoadingPage");
    }
}
